package com.lanou.beautifulwardrobe.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.lanou.beautifulwardrobe.dbHelper.UsersDBHelper;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.view.RegularUtil;
import com.lanou.beautifulwardrobe.contants.ToastCommom;
import com.lanou.beautifulwardrobe.entity.greendao.UsersEntity;

/**
 * Created by dllo on 15/11/4.
 */
public class RequsetActivity extends Activity implements View.OnClickListener {
    private EditText requsetUser, requsetPwd, requsetAgainPwd;
    private ImageButton requsetBack;
    private Button requsetBtn;
    private UsersDBHelper helper = UsersDBHelper.getInstance();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_requsetpwd);
        initView();
    }

    private void initView() {
        requsetUser = (EditText) findViewById(R.id.requsetUser);
        requsetPwd = (EditText) findViewById(R.id.requsetPwd);
        requsetAgainPwd = (EditText) findViewById(R.id.requsetAgainPwd);
        requsetBack = (ImageButton) findViewById(R.id.requsetBack);
        requsetBtn = (Button) findViewById(R.id.requsetBtn);
        listener();
    }

    private void listener() {
        requsetBack.setOnClickListener(this);
        requsetBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.requsetBack:
                finish();
                break;
            case R.id.requsetBtn:
                if (helper.isSaved(requsetUser.getText().toString().trim())) {
                    if (RegularUtil.checkPassword(this, requsetPwd.getText().toString().trim())
                            && RegularUtil.checkPassward(this, requsetPwd.getText().toString().trim(),
                            requsetAgainPwd.getText().toString().trim())) {
                        UsersEntity usersEntity = helper.queryByUser(requsetUser.getText().toString().trim());
                        if (usersEntity != null ) {
                            Log.i("lanou","/*/*/*/*/*/*/*/*/*/*/*");
                          //  UsersEntity usersEntity = usersEntities.get(0);
                            usersEntity.setPwd(requsetPwd.getText().toString().trim());
                            helper.upDate(usersEntity);
                            ToastCommom.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }
                } else {
                    ToastCommom.makeText(this, "用户名不存在", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
